<?php
declare (strict_types=1);

namespace app\admin\controller;

use app\BaseController;
use app\model\CourseCategory;
use think\Request;

class Coursecate extends BaseController
{
    /**
     * 显示资源列表
     * 课程分類
     * @return \think\Response
     */
    public function index($limit = 10)
    {


        $name = request()->param('name');
        $status = request()->param('status');
        $time = request()->param('time');
        $data= CourseCategory::order('sort', 'desc')
            ->when($name, function ($query) use ($name) {
                $query->where('name', 'like', "%$name%");
            })->when($status, function ($query) use ($status) {
                $query->where('status', $status);
            })->when($time, function ($query) use ($time) {
                $query->whereBetween('create_time', $time);
            })->paginate($limit);
        return $this->success($data);
    }

    /**
     * 保存新建的资源
     *
     * @param \think\Request $request
     * @return \think\Response
     */
    public function create(Request $request)
    {
        CourseCategory::create($request->param());
        return $this->success();
    }


    /**
     * 保存更新的资源
     *
     * @param \think\Request $request
     * @param int $id
     * @return \think\Response
     */
    public function update(Request $request, $id)
    {
        CourseCategory::where('id', $id)->update($request->param());
        return $this->success();
    }

    /**
     * 删除指定资源
     *
     * @param int $id
     * @return \think\Response
     */
    public function delete($id)
    {
        CourseCategory::where('id', 'in', $id)->delete();
        return $this->success();
    }
}
